package com.awesome.spring.mqtt.controller;

/**
 * @program: awesome-spring
 * @description:
 * @author: Alex Wu
 * @createDate: 2025-03-19 10:28
 **/
import com.awesome.spring.mqtt.service.MqttService;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mqtt")
public class MqttController {

    @Autowired
    private  MqttService mqttService;

    @PostMapping("/send")
    public String sendMessage(@RequestParam String message, @RequestParam(defaultValue = "test/topic") String topic) {
        try {
            mqttService.publish(topic, message);
            return "消息已发送：" + message;
        } catch (MqttException e) {
            return "消息发送失败：" + e.getMessage();
        }
    }
}
